package com.ywl.study.sharding.study01;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.*;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class DataSourceTest {
    /***
     * ShardingRuleConfiguration:
     * tables:
     *   t_order:
     *     actualDataNodes: ds${0..1}.t_order${0..1}
     *     databaseStrategy:
     *       inline:
     *         algorithmExpression: ds${user_id % 2}
     *         shardingColumn: user_id
     *     logicTable: t_order
     *     tableStrategy:
     *       inline:
     *         algorithmExpression: t_order${order_id % 2}
     *         shardingColumn: order_id
     *   t_order_item:
     *     actualDataNodes: ds${0..1}.t_order_item${0..1}
     *     databaseStrategy:
     *       inline:
     *         algorithmExpression: ds${user_id % 2}
     *         shardingColumn: user_id
     *     logicTable: t_order_item
     *     tableStrategy:
     *       inline:
     *         algorithmExpression: t_order_item${order_id % 2}
     *         shardingColumn: order_id
     */
    public static void main(String[] args) throws SQLException {

        Map<String, DataSource> dataSourceMap = new HashMap<>();

        // 配置第一个数据源
        BasicDataSource dataSource1 = new BasicDataSource();
        dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource1.setUrl("jdbc:mysql://localhost:3306/ds0");
        dataSource1.setUsername("root");
        dataSource1.setPassword("123456");
        dataSourceMap.put("ds0", dataSource1);

        // 配置第二个数据源
        BasicDataSource dataSource2 = new BasicDataSource();
        dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource2.setUrl("jdbc:mysql://localhost:3306/ds1");
        dataSource2.setUsername("root");
        dataSource2.setPassword("123456");
        dataSourceMap.put("ds1", dataSource2);
        // 配置Order表规则
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("t_order","ds${0..1}.t_order${0..1}");

        // 配置分库 + 分表策略
        orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
        orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order${order_id % 2}"));

        // 配置分片规则
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);

        // 省略配置order_item表规则...
        TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration("t_order_item","ds${0..1}.t_order_item${0..1}");

        // 配置分库 + 分表策略
        orderItemTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
        orderItemTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_item${order_id % 2}"));

        // 配置分片规则
        shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);





        // 获取数据源对象
        DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties());
        String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
        Connection connection=dataSource.getConnection();
//        String insertSql="insert into t_order values(?,?)";
//        String insertItemSql="insert into t_order_item values(?,?)";
//        for(int i=0;i<100;i++){
//            PreparedStatement insertStatement=connection.prepareStatement(insertSql);
//            insertStatement.setInt(1,i+1);
//            insertStatement.setInt(2,1000+i);
//            insertStatement.execute();
//            PreparedStatement insertItemStatement=connection.prepareStatement(insertItemSql);
//            insertItemStatement.setInt(1,i+1);
//            insertItemStatement.setString(2,"产品");
//            insertItemStatement.execute();
//        }

        PreparedStatement statement=connection.prepareStatement("SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?");
        statement.setInt(1,1000);
        statement.setInt(2,1);
        ResultSet resultSet=statement.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getInt(1));
            System.out.println(resultSet.getString(2));
        }



    }


}
